package com.example.demo.common.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.entity.Sample;
import com.example.demo.common.mapper.SampleMapper;
import com.example.demo.common.service.ExclService;
import com.example.demo.common.service.SampleService;
import com.example.demo.common.serviceImpl.ExclServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/samp")
public class SampController {
    @Resource
    SampleMapper sampleMapper;
    @Resource
    SampleService sampleService;
    @Resource
    ExclServiceImpl exclServiceimpl;

//    此注解为增加
    @PostMapping
    public Result<?> save(@RequestBody Sample sample){
        System.out.println(sample);
        Date date = new Date();
        sample.setInsert_time(date);
        Integer num = sampleService.count_table();
        sample.setNum(num+1);
        sampleService.saveSample(sample);
        return Result.success();
    }
//    此注解为更新
    @PutMapping
    public Result<?> update(@RequestBody Sample sample){
        sampleService.updateSample(sample);
        return Result.success();
    }
//    此注解为删除
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){
        sampleService.deleteById(id);
        return Result.success();
    }
//    此注解为查询
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Sample> wrapper = Wrappers.<Sample>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Sample::getRef, search);
        }
        Page<Sample> samplePage = sampleMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(samplePage);
    }
    @PostMapping("/excl")
    public Result<?> excl(@RequestBody String string){
        System.out.println(string);
        List<Map<String,Object>> result =exclServiceimpl.ReadExcelByRC("E:\\test\\1\\"+string,0,-1,false);
        for (Integer i=0;i <result.size();i++){
            List samp = new ArrayList();
            Sample sample = new Sample();
            for(String t: result.get(i).keySet())
            {

                System.out.println( result.get(i).get(t) );
                samp.add(result.get(i).get(t));
            }
            System.out.println(samp.get(0).toString());
            if (samp.get(0)!=null){
                sample.setRef(samp.get(0).toString());
            }
            if (samp.size()>=2 && samp.get(1)!=null){
                sample.setName(samp.get(1).toString());
            }
            System.out.println(samp.size());
            if (samp.size()>=3 && samp.get(2)!=null){
                sample.setDes(samp.get(2).toString());
            }
            Date date = new Date();
            sample.setInsert_time(date);
            Integer num = sampleService.count_table();
            sample.setNum(num+1);
            System.out.println(sample);
            sampleService.saveSample(sample);

        }

        return Result.success();
    }

}
